GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

directive.range.js ➔ RangeConstructor   F
last analyzed

Complexity

Conditions 47

Size

Total Lines 194
Code Lines 122

Duplication

Lines 70
Ratio 36.08 %

Importance

Changes 0
Metric Value
cc 47
eloc 122
dl 70
loc 194
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like directive.range.js ➔ RangeConstructor often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
2
app.directive('range', ['$rootScope',
3
    '$timeout',
4
    'Core',
5
    'Security',
6
    'MessageService', function($rootScope, $timeout, Core, Security, MessageService) {
7
    function RangeConstructor($scope, $element, $attrs) {
8
        var constructor = this;
0 ignored issues
show
Unused Code introduced by
The variable constructor seems to be never used. Consider removing it.
Loading history...
9
        var $ctrl = $scope.rangeCtrl;
10
        var tagName = $element[0].tagName.toLowerCase();
11
12 View Code Duplication
        var DirectiveProperties = (function () {
13
            var multi;
14
15
            function findMultiable(){
16
                var object = $attrs.multi;
17
                if(typeof(object) != "undefined")
18
                    return true;
19
                else
20
                    return false;
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
21
            }
22
23
            return {
24
                getMultiable: function () {
25
                    if (!multi) {
26
                        multi = findMultiable();
27
                    }
28
                    $scope.multi = multi;
29
                    return multi;
30
                }
31
            };
32
        })();
33
34
        function TryToCallInitDirective(){
35
            if(typeof $scope.InitDirective == "function"){
36
                $scope.InitDirective($scope, $element, $attrs, $ctrl);
37
            }else{
38
                $scope.DefaultInitDirective();
39
            }
40
        }
41
        $scope.DefaultInitDirective = function(){
42
            if(Core.GetConfig().debugLog.DirectiveFlow)
43
            console.log("scope.$id:"+$scope.$id+", may implement $scope.InitDirective() function in webapge");
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
44
        }
45
46
        $scope.IsRange = function()
47
        {
48
            return true;
49
        }
50
51
        $scope.GetInitRange = function(){
52
            var range = {start: "ALL", end: ""};
53
54
            return range;
55
        }
56
57
        function InitializeRange() {
58
            DirectiveProperties.getMultiable();
59
60
            var range = {start: "ALL", end: "", isAll: false};
61
            $ctrl.ngModel = jQuery.extend({}, range);
62
            // $ctrl.ngModel.isAll = true;
63
        }
64
        $scope.Initialize = function(){
65
            $scope.InitScope();
66
            if(typeof $scope.EventListener == "function"){
67
                $scope.EventListener($scope, $element, $attrs, $ctrl);
68
            }else{
69
                EventListener();
70
            }
71
            TryToCallInitDirective();
72
        }
73
        $scope.InitScope = function(){
74
            InitializeRange();
75
        }
76
77
        function InitDirective(){
78
            if(Core.GetConfig().debugLog.DirectiveFlow)
79
            console.log("scope.$id:"+$scope.$id+", may implement $scope.InitDirective() function in webapge");
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
80
        }
81
        function EventListener(){
82
            if(Core.GetConfig().debugLog.DirectiveFlow)
83
            console.log("scope.$id:"+$scope.$id+", may implement $scope.EventListener() function in webapge");
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
84
        }
85
86
        // chagneRagne = [start | end]
87 View Code Duplication
        function RangeStringChange(changeRange, ctrlModel){
88
            var isLocaleCompareSupport = localeCompareSupportsLocales();
89
90
            var strStart = ctrlModel.start;
91
            var strEnd = ctrlModel.end;
92
            var stringDifference = 0;
93
94
            // if user change the start value
95
            if(strStart != "ALL"){
96
                $ctrl.ngModel.isAll = false;
97
            }
98
99
            // string comparison, find the strStart position of strEnd
100
            if(isLocaleCompareSupport){
101
                 stringDifference = strStart.localeCompare(strEnd);
102
            }else{
103
                if(strStart < strEnd)
104
                    stringDifference = -1;
105
                if(strEnd < strStart)
106
                    stringDifference = 1;
107
                if(strStart == strEnd)
108
                    stringDifference = 0;
109
            }
110
111
            if(changeRange == "start"){
112
113
                if(stringDifference > 0)
114
                {
115
                    strEnd = strStart
116
                }
117
                if(stringDifference < 0)
118
                {
0 ignored issues
show
Comprehensibility Documentation Best Practice introduced by
This code block is empty. Consider removing it or adding a comment to explain.
Loading history...
119
120
                }
121
            }else if(changeRange == "end"){
122
                if(stringDifference > 0)
123
                {
124
                    strStart = strEnd
125
                }
126
                if(stringDifference < 0)
127
                {
128
                    if(strStart == "")
129
                        strStart = strEnd
130
                }
131
            }
132
133
            ctrlModel.start = strStart;
134
            ctrlModel.end = strEnd;
135
        }
136
137
        function localeCompareSupportsLocales() {
138
          try {
139
            'foo'.localeCompare('bar', 'i');
140
          } catch (e) {
141
            return e.name === 'RangeError';
142
          }
143
          return false;
144
        }
145
146
        $scope.SetRange = function(rangeType, value){
147
            if(rangeType == "start"){
148
                $ctrl.ngModel.start = value;
149
            }else if(rangeType == "end"){
150
                $ctrl.ngModel.end = value;
151
            }
152
            
153
            RangeStringChange(rangeType, $ctrl.ngModel)
154
        }
155
156
        $scope.GetRange = function(){
157
            var rangeList = [];
158
            var range = rangeList[0] = {start: "", end: ""};
159
160
            range.start = $ctrl.ngModel.start;
161
            range.end = $ctrl.ngModel.end;
162
163
            return range;
164
        }
165
        $scope.IsLockEndControl = function(){
166
            var isLock = false;
167
            var range = $scope.GetRange();
168
            if(range.start == "ALL"){
169
                isLock = true;
170
            }
171
            return isLock;
172
        }
173
174
        $scope.CheckAllRange = function(test){
175
            console.dir("CheckAllRange() function")
176
        }
177
        
178
        $scope.$watch(
179
          function() { return $ctrl.ngModel.isAll; },
180
          function(newValue, oldValue) {
181
              var isCheckAll = newValue;
182
              
183
            if(isCheckAll){
184
                $ctrl.ngModel.start = "ALL"
185
                $ctrl.ngModel.end = ""
186
187
                var editBtn = $element.find('button[ng-click="OpenPageView()"]').last();
188
                editBtn.prop('disabled', true);
189
            }
190
            else{
191
                if($ctrl.ngModel.start == "ALL")
192
                    $ctrl.ngModel.start = ""
193
194
                var editBtn = $element.find('button[ng-click="OpenPageView()"]').last();
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable editBtn already seems to be declared on line 187. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
195
                editBtn.prop('disabled', false);
196
            }
197
          },
198
          true
199
        );
200
    }
201
    function templateFunction(tElement, tAttrs) {
202
        var globalCriteria = $rootScope.globalCriteria;
203
204
        var template = '' +
205
          '<div class="custom-transclude"></div>';
206
        return template;
207
    }
208
209
    return {
210
        require: ['ngModel'],
211
        restrict: 'E', //'EA', //Default in 1.3+
212
        transclude: true,
213
        scope: true,
214
215
        controller: RangeConstructor,
216
        controllerAs: 'rangeCtrl',
217
218
        //If both bindToController and scope are defined and have object hashes, bindToController overrides scope.
219
        bindToController: {
220
            ngModel: '=',
221
        },
222
        template: templateFunction,
223
        compile: function compile(tElement, tAttrs, transclude) {
224
            return {
225
                pre: function preLink(scope, iElement, iAttrs, controller) {
226
                    //console.log("range preLink() compile");
227
                },
228
                post: function postLink(scope, iElement, iAttrs, controller) {
229
                    //console.log("range postLink() compile");
230
231
                    transclude(scope, function (clone, scope) {
232
                        iElement.find('.custom-transclude').append(clone);
233
                    })
234
                    
235
                    scope.Initialize();
236
                    iElement.ready(function() {
237
                        scope.rangeCtrl.ngModel.start = "ALL";
238
                        scope.rangeCtrl.ngModel.isAll = true;
239
                    })
240
                }
241
            }
242
        },
243
    };
244
}]);